package com.app.business.web.business;

import com.app.business.model.*;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.log4j2.LogUtils;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.exception.Precondition;
import freemarker.template.Configuration;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 统计查询专用
 * Created by ZYK on 2019/2/22.
 *
 * @author zyk
 */
@Controller
@RequestMapping("/business/statistics/statistics/*")
@SuppressWarnings("all")
public class StatisticsController {

    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private StatisticsForYearService statisticsForYearService;
    private Logger logger = LogUtils.getBusinessLogger();
    @Autowired
    private Configuration freemarkerConfiguration; // 注入bean，这里是属性注入，推荐构造注入

    /**
     * 征收项目统计
     *
     * @return
     */
    @RequestMapping("zsProjectStatstics")
    @RequiresPermissions("statistics:zsProjectStatstics")
    public void contractSumByZsProject(ModelMap modelMap) {
        Map<String, Map> resultMap = this.statisticsService.getContractSumByZsProjectMap();
        modelMap.addAttribute("resultMap", resultMap);
    }

    /**
     * 安置房源统计
     * 各个安置项目房源、车位、储物间选房情况
     */
    @RequestMapping("azStatistics")
    @RequiresPermissions("statistics:azStatistics")
    public void azStatistics(ModelMap modelMap) {
        //安置项目集合
        Map<String, DataDict> townMap = dataDictService.getMapByParentNameForName("乡镇");

        UserInfo userInfo = WebUtils.getLoginUserInfo();
        String town = null;
        String townStr = null;
        String townCode = null;
        if (userInfo.getDepartment().getCode() != null && !userInfo.getDepartment().getCode().equals("001")) {
            town = userInfo.getDepartment().getName();
            townStr = town.substring(0, 2) + "片区";
            townCode = townMap.get(townStr).getValue();
        }
        List<AzProject> azProjectList = this.azProjectService.azProjectByTownList(townCode);

        // Map<azProjectId,Map<buildNum，Map<全部，已选，未选>>>
        Map<String, Map<String, Map>> houseMap = this.statisticsService.getHouseCountMapByAzProjectId();
        // Map<azProjectId,Map<全部，已选，未选>>
        Map<String, Map> carLotMap = this.statisticsService.getCarLotCountMapByAzProjectId();
        // Map<azProjectId,Map<全部，已选，未选>>
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomCountMapByAzProjectId();
        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("carLotMap", carLotMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("bigUtils", new BigDecimalUtils());
    }


    /**
     * 选房情况汇总表
     */
    @RequestMapping("chooseHouseStatistics")
    @RequiresPermissions("statistics:chooseHouseStatistics")
    public void chooseHouseStatistics(ModelMap modelMap) {
        //安置项目集合
        Map<String, DataDict> townMap = dataDictService.getMapByParentNameForName("乡镇");

        UserInfo userInfo = WebUtils.getLoginUserInfo();
        String town = null;
        String townStr = null;
        String townCode = null;
        if (userInfo.getDepartment().getCode() != null && !userInfo.getDepartment().getCode().equals("001")) {
            town = userInfo.getDepartment().getName();
            townStr = town.substring(0, 2) + "片区";
            townCode = townMap.get(townStr).getValue();
        }
        List<AzProject> azProjectList = this.azProjectService.azProjectByTownList(townCode);

        Map<String, Map> contractMap = this.statisticsService.getContractStatisticsByAzProjectId();
        Map<String, Map> houseMap = this.statisticsService.getHouseStatisticsByAzProjectId();
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomCountMapByAzProjectId();
        Map<String, Map> carLotMap = this.statisticsService.getCarLotCountMapByAzProjectId();
        Map<String, Integer> countMap = this.statisticsService.getZsProjectCountByAzProject();

        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("countMap", countMap);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("carLotMap", carLotMap);
        modelMap.addAttribute("bigUtils", new BigDecimalUtils());

    }


    /**
     * 选房情况汇总表
     * 每个安置项目具体情况
     */
    @RequestMapping("chooseHouseZsStatistics")
    @RequiresPermissions("statistics:chooseHouseZsStatistics")
    public void chooseHouseAzStatistics(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkSqdsArguement(azProject != null, "安置项目不存在！");
        String zsProjectIdStr = this.statisticsService.findZsProjectIdSetByAzProjectId(azProject.getId());
        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByZsProjectIdStr(zsProjectIdStr);

        Map<String, Map> contractMap = this.statisticsService.getContractZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> houseMap = this.statisticsService.getHouseZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> carLotMap = this.statisticsService.getCarLotZsStatisticsByAzProjectId(azProject.getId());
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }

    /**
     * 选房情况一览表
     */
    @RequestMapping("chooseHouseProgressAzStatistics")
    @RequiresPermissions("statistics:chooseHouseProgressAzStatistics")
    public void chooseHouseProgressAzStatistics(ModelMap modelMap) {
        //安置项目集合
        Map<String, DataDict> townMap = dataDictService.getMapByParentNameForName("乡镇");

        UserInfo userInfo = WebUtils.getLoginUserInfo();
        String town = null;
        String townStr = null;
        String townCode = null;
        if (userInfo.getDepartment().getCode() != null && !userInfo.getDepartment().getCode().equals("001")) {
            town = userInfo.getDepartment().getName();
            townStr = town.substring(0, 2) + "片区";
            townCode = townMap.get(townStr).getValue();
        }
        List<AzProject> azProjectList = this.azProjectService.azProjectByTownList(townCode);

        Map<String, Map> contractMap = this.statisticsService.getContractProgressStatisticsByAzProjectId();
        Map<String, Map> houseMap = this.statisticsService.getHouseProgressStatisticsByAzProjectId();

        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);

    }

    /**
     * 选房情况一览表
     * 安置区内征收项目统计
     */
    @RequestMapping("chooseHouseProgressZsStatistics")
    @RequiresPermissions("statistics:chooseHouseProgressZsStatistics")
    public void chooseHouseProgressZsStatistics(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkSqdsArguement(azProject != null, "安置项目不存在！");
        String zsProjectIdStr = this.statisticsService.findZsProjectIdSetByAzProjectId(azProject.getId());
        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByZsProjectIdStr(zsProjectIdStr);

        Map<String, Map> contractMap = this.statisticsService.getContractProgressZsStatisticsByAzProjectId(azProjectId);
        Map<String, Map> houseMap = this.statisticsService.getHouseProgressZsStatisticsByAzProjectId(azProjectId);

        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);

    }


    /**
     * 协议详细查看界面
     */
    @RequestMapping("houseShow")
    @RequiresPermissions("statistics:houseShow")
    public void houseShow(HttpServletRequest request, ModelMap modelMap) {
        PageBean<House> housePageBean = this.statisticsService.getStatisticsSelectedHousePageBean(request);
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");

        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
    }

    /**
     * 协议详细查看界面
     */
    @RequestMapping("contractShow")
    @RequiresPermissions("statistics:contractShow")
    public void contractShow(HttpServletRequest request, ModelMap modelMap) {

        List<House> houseList = this.statisticsService.getStatisticsSelectedContractList(request);
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");

        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
    }


}
